package com.dingding.common.core.util.tools.QR;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.dingding.common.core.util.UrlMaker;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class QRTool {
	
	private static final Logger log = Logger.getLogger(QRTool.class);
	
	public static void createQr(String couponId,OutputStream output)
	{
		try {
			String content = UrlMaker.getQRcodeLinkUrl(couponId);// 内容
			int width = 500; // 图像宽度
			int height = 500; // 图像高度
			String format = "png";// 图像类型
			Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width,height, hints);// 生成矩阵
			MatrixToImageWriter.writeToStream(bitMatrix, format, output);// 输出图像
		} catch (Exception e) {
			log.error("生成二维码的时候出现错误！QRTool：createQr",e);
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				log.error("生成二维码的时候出现错误！QRTool：createQr",e);
			}
		}
	}
}
